package gu.simplemq.mqtt;

import java.util.Properties;

import gu.simplemq.MQConstProvider;
import gu.simplemq.MQProperties;
import gu.simplemq.MQPropertiesHelper;
import gu.simplemq.pool.BaseMQPool.MQPoolException;

public class PropertiesHelper extends MQPropertiesHelper {
	public static final PropertiesHelper MHELPER = new PropertiesHelper();

	PropertiesHelper() {
	}

	@Override
	public MQConstProvider getConstProvider() {
		return MqttConstProvider.MPROVIDER;
	}

	@Override
	public void checkConnect(Properties input, Integer timeoutMills) {
		MQProperties props = initParameters(input);
	    MqttPoolLazy pool = MqttPoolLazys.getInstance(props);
	    try {
	    	pool.apply();
			pool.free();
		} catch (MQPoolException e) {
			// 连接失败的对象直接关闭
			pool.close();
			throw e;
		}
	}

	@Override
	public boolean testConnect(Properties props, Integer timeoutMills) {
		try {
			checkConnect(props, timeoutMills);
			return true;
		} catch (MQPoolException je) {	      
		      return false;
		}
	}
}
